import Vue from 'vue';
//引入vuex
import vuex from 'vuex';
import servcie from '../utils/servcie';
Vue.use(vuex)
export default new vuex.Store({
    state: {
        count:1,
        age: 12,
        isAdult:false,
    },
    getters: {
       isAdult: function(state) {
        return state.isAdult?"已成年":"未成年"
       }
    },
    mutations: {
        //state：当前仓库的state\
        //paload:载荷==>提交的每次改变的参数
        maddCount(state,payload){
            // console.log(payload);
            state.count+=payload
        },
        maddage(state,payload){
            // console.log('payload: ', payload);
            state.age+=payload.msg;
            if(state.age>=18){
                this.state.isAdult=true;
            }else{
                this.state.isAdult=false;
            }
        }
    },
    actions: {
        //context:上下文对象==>就是所创建的store对象
        aAddage(context){
            servcie({
                url:"http://localhost:3000/inc",
            }).then(res=>{
                //提交mutation
                context.commit({
                    type:"maddage",
                    msg:res.data.count
                });
                // console.log(res.data.count);
            })
        }
    },
})